# -*- coding: utf-8 -*-
import sys, os
try:
        import pygtk
        pygtk.require("2.0")
except:
        pass
try:
        import gtk
        import gtk.glade
except:
        sys.exit(1)

from simpledcpp.core.dcpp import DCPPPart
class SettingsPage(DCPPPart):
	def activate(self, *args, **kwargs):
		pass
	def __init__(self, parent):
		self._speedlist = ['LAN 10Mbit','LAN 100Mbit','LAN 1Gbit','xDSL','Modem 56k']
		self.type = "Settings"
		DCPPPart.__init__(self, parent)
		self.name = "Параметры"
		self._widget = gtk.VBox()
#	self._widget.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
		self._SVBOX = self.parent.wtree.get_widget('SettingsVBOX')
		self._SVBOX.reparent(self._widget)
#		self._widget.pack_start(self._SVBOX)
		self.parent._fastConnect("SettingsOk", "clicked", self._ok)
		self._widget.show()
		self._load()

	def _ok(self, *args, **kwargs):
		self.settings.set('nickname', self.parent.wtree.get_widget("SettingsNickname").get_text())
		self.settings.set('password', self.parent.wtree.get_widget("SettingsPassword").get_text().strip())
		self.settings.set('infointerest', self.parent.wtree.get_widget("SettingsInterest").get_text())
		self.settings.set('infoemail', self.parent.wtree.get_widget("SettingsEmail").get_text())
		self.settings.set('infospeed', self._speedlist[self.parent.wtree.get_widget("SettingsSpeed").get_active()])
		self.settings.set('downloads', self.parent.wtree.get_widget("SettingsFinished").get_current_folder())
		self.settings.set('unfinished', self.parent.wtree.get_widget("SettingsUnfinished").get_current_folder())
		#Second tab
                self.settings.set("upload_speed",int(self.parent.wtree.get_widget("SettingsUploadSpeed").get_value()).__str__())
                self.settings.set("download_speed",int(self.parent.wtree.get_widget("SettingsDownloadSpeed").get_value()).__str__())
                self.settings.set("upload_slots",int(self.parent.wtree.get_widget("SettingsUploadSlots").get_value()).__str__())
                self.settings.set("download_slots",int(self.parent.wtree.get_widget("SettingsDownloadSlots").get_value()).__str__())
                self.settings.set("local_ip",self.parent.wtree.get_widget("SettingsIP").get_text())
                self.settings.set("local_port",self.parent.wtree.get_widget("SettingsPort").get_text())
		#Third Tab
		self.settings.set("segment_size",int(self.parent.wtree.get_widget("SettingsSegmentSize").get_value()).__str__())
		self.settings.set("auto_search",int(self.parent.wtree.get_widget("SettingsAutoSearch").get_value()).__str__())
		self.settings.set("check_tth",int(self.parent.wtree.get_widget("SettingsCheckTTH").get_active()).__str__())
		#Fourth tab
		self.settings.set('NotifyEnabled', int(self.parent.wtree.get_widget("NotifyEnabled").get_active()).__str__())
		self.settings.set("NotifyFile", int(self.parent.wtree.get_widget("NotifyFile").get_active()).__str__())
		self.settings.set("NotifyMessage", int(self.parent.wtree.get_widget("NotifyMessage").get_active()).__str__())
		self.settings.set("NotifyTime", int(self.parent.wtree.get_widget("NotifyTime").get_value()).__str__())

	def _load(self):
		self.parent.wtree.get_widget("SettingsNickname").set_text(self.settings.get('nickname', ""))
		self.parent.wtree.get_widget("SettingsPassword").set_text(self.settings.get('password', ""))
		self.parent.wtree.get_widget("SettingsInterest").set_text(self.settings.get('infointerest',""))
		self.parent.wtree.get_widget("SettingsEmail").set_text(self.settings.get("infoemail",""))
		speed = self.settings.get("infospeed")
		if speed in self._speedlist:
			self.parent.wtree.get_widget("SettingsSpeed").set_active(self._speedlist.index(speed))
		else:
			self.parent.wtree.get_widget("SettingsSpeed").set_active(0)
		self.parent.wtree.get_widget("SettingsFinished").set_current_folder(self.settings.get('downloads',"/"))
		self.parent.wtree.get_widget("SettingsUnfinished").set_current_folder(self.settings.get('unfinished', "/"))
		#Second tab
		self.parent.wtree.get_widget("SettingsUploadSpeed").set_value(int(self.settings.get("upload_speed","100000")))
		self.parent.wtree.get_widget("SettingsDownloadSpeed").set_value(int(self.settings.get("download_speed","100000")))
		self.parent.wtree.get_widget("SettingsUploadSlots").set_value(int(self.settings.get("upload_slots","5")))
		self.parent.wtree.get_widget("SettingsDownloadSlots").set_value(int(self.settings.get("download_slots","5")))
		self.parent.wtree.get_widget("SettingsIP").set_text(self.settings.get("local_ip","127.0.0.1"))
		self.parent.wtree.get_widget("SettingsPort").set_text(self.settings.get("local_port","31331"))
		#Third tab
		self.parent.wtree.get_widget("SettingsAutoSearch").set_value(int(self.settings.get("auto_search","100")))
		self.parent.wtree.get_widget("SettingsSegmentSize").set_value(int(self.settings.get("segment_size","1000000")))
		self.parent.wtree.get_widget("SettingsCheckTTH").set_active(bool(
								int(self.settings.get("check_tth",0))
								))
		#Fourth tab
		enabled = int(self.settings.get("NotifyEnabled",1))
		file = int(self.settings.get("NotifyFile",1))
		message = int(self.settings.get("NotifyMessage",1))
		timeout = int(self.settings.get("NotifyTime",10))
	
		self.parent.wtree.get_widget("NotifyEnabled").set_active(bool(enabled))
		self.parent.wtree.get_widget("NotifyFile").set_active(bool(file))
		self.parent.wtree.get_widget("NotifyMessage").set_active(bool(message))
		self.parent.wtree.get_widget("NotifyTime").set_value(int(timeout))

	
	def Close(self):
		pass	
